package com.itboye.lottery.activity.user;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.adapter.AddressListAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.AddressBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class AddressListActivity extends BaseActivity implements OnItemClickListener, ResponseCallback {

    RecyclerView rv_address_list;
    ArrayList<AddressBean.AddressItem> arrayList;
    private AddressListAdapter adapter;
    UserPresenter userPresenter;
    TextView tv_actionbar_right;
    Button btn_choose_post_address;
    private String addressId;

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        rv_address_list.setLayoutManager(new LinearLayoutManager(this));
        arrayList = new ArrayList<>();
        adapter = new AddressListAdapter(this, arrayList);
        adapter.setOnItemClickListener(this);
        rv_address_list.setAdapter(adapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_list;
    }

    @Override
    public void onClick(View view) {
        if (view == tv_actionbar_right) {
            startActivity(new Intent(this, AddressEditActivity.class));
        } else if (view == btn_choose_post_address) {
            addressId = "";
            for (AddressBean.AddressItem addressBean : arrayList) {
                if (addressBean.getIs_default() == 1) {
                    addressId = addressBean.getAddress_id();
                    break;
                }
            }
            userPresenter.chooseAddress(addressId);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (view.getId()) {
            case R.id.llRevice:
                startActivity(new Intent(this, AddressEditActivity.class).putExtra("model", arrayList.get(position)));
                break;
            case R.id.llDelete:
                userPresenter.deleteAddress(arrayList.get(position).getAddress_id());
                break;
            case R.id.addressRadio:
                for (int i = 0; i < arrayList.size(); i++) {
                    arrayList.get(i).setIs_default(0);
                }
                arrayList.get(position).setIs_default(1);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    private void getAddressList() {
        userPresenter.getAddressList("");
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.getAddressList_SUCCESS) {
            arrayList.clear();
            arrayList.addAll(((AddressBean) o).getList());
            adapter.notifyDataSetChanged();
        } else if (requestSuccessType == userPresenter.deleteAddress_SUCCESS) {
            ByAlert.alert(o);
            getAddressList();
        }else if (requestSuccessType == userPresenter.chooseAddress_SUCCESS) {
            ByAlert.alert(o);
            finish();
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        ByAlert.alert(msg);
    }

    public void sure(View view) {

    }
}
